#!/bin/bash
Url=http://mirror.cnop.net
echo -e "\033[32m  .......... Starting install................ \033[0m"
echo ""
Core=`cat /proc/cpuinfo| grep "processor"| wc -l`
echo "Cpu core number $Core"
Memory_total=`free  |grep -i mem |gawk -F : '{print $2}' |gawk '{print $1}'`
if (("$Memory_total" < "1000000"))
then
   echo ""
   echo "Your memory is too low suggest installation 5.6"
   else
   echo ""
fi

if (("$Memory_total" < "500000"))
then
   echo -e "\033[32m  disable-fileinfo \033[0m"
   Fileinfo="--disable-fileinfo"
   else
   Fileinfo=""
fi

echo -e "\033[32m Enter a PHP path( Default /usr/local/php ):   \033[0m"
read Path
if [ -z "$Path" ]; then
  echo -e "\033[32m ....Using the default path /usr/local/php .... \033[0m"
  Path=/usr/local/php
  echo $Path
else
  echo -e "\033[32m ....... Your input $Path ........ \033[0m"
  echo $Path
fi

#php_version
echo " " && echo -e "\033[32m Please enter a PHP version using Numbers instead: \033[0m"
echo " " && echo -e "\033[32m ... Memory with php7+ must not be less than 1G ... \033[0m"
echo -e "\033[32m 1.php-5.6.0   \033[0m"
echo -e "\033[32m 2.php-7.0.33   \033[0m"
echo -e "\033[32m 3.php-7.2.8 (Default) \033[0m"
echo -e "\033[32m 4.php-7.3.2    \033[0m"
echo -e "\033[32m 5.php-7.4.3   \033[0m"
echo -e "\033[32m 6.php-7.4.28   \033[0m"
read Php_version
useradd www
case $Php_version in
1)
    echo php-5.6.0	
	Php_version=php-5.6.0
    ;;
2)
    echo php-7.0.33
	Php_version=php-7.0.33
    ;;
3|'')
    echo php-7.2.8 
    Php_version=php-7.2.8
	sleep 2
    ;;
4)
    echo php-7.3.2
    Php_version=php-7.3.2
	sleep 2
    ;;
5)
    echo php-7.4.3
    Php_version=php-7.4.3
	sleep 2
    ;;
6)
    echo php-7.4.28
    Php_version=php-7.4.28
	sleep 2
    ;;
esac

yum install -y epel-release
yum install -y make cmake wget gcc gcc-c++ autoconf automake libpng libpng-devel libjpeg libjpeg-devel zlib libxml2-devel ncurses-devel bison libtool-ltdl-devel libmcrypt mhash mcrypt libmcrypt-devel pcre-devel openssl-devel freetype freetype-devel libcurl-devel bzip2 bzip2-devel re2c libzip libXpm-devel libwebp libwebp-devel libicu-devel libxslt libxslt-devel php-mysql ImageMagick ImageMagick-devel

Php_num=`echo $Php_version | cut -b 5,7`

# if [  $Php_num -ge 72 ]
# then 
    #>=7.2 install Module
	# Mcrypt_Module="--with-mcrypt"
	# else
	# Mcrypt_Module=""
# fi

if [  $Php_num -ge 73 ]
then 
	yum remove libzip -y	
	yum install sqlite-devel oniguruma oniguruma-devel -y
	cd /opt && wget $Url/lib/libzip/libzip-1.2.0.tar.gz
    tar -zxvf libzip-1.2.0.tar.gz
    cd libzip-1.2.0
    ./configure
    make -j$Core  && make -j$Core install
	echo '/usr/local/lib64'>>/etc/ld.so.conf
    echo '/usr/local/lib'>>/etc/ld.so.conf
    echo '/usr/lib'>>/etc/ld.so.conf
    echo '/usr/lib64'>>/etc/ld.so.conf	
	/sbin/ldconfig -v
	cp /usr/local/lib/libzip/include/zipconf.h /usr/local/include/zipconf.h
	
	echo " " && echo -e "\033[32m Use cmake3: \033[0m"
	cd /opt && wget $Url/lib/cmake/cmake-3.9.2.tar.gz
    tar zxvf cmake-3.9.2.tar.gz
    cd cmake-3.9.2 && ./configure
    make -j$Core  && make -j$Core install
    hash -r && cmake -version
fi


echo " " && echo -e "\033[32m Install libiconv: \033[0m"
cd /opt && wget $Url/lib/libiconv/libiconv-1.16.tar.gz
tar -zxvf libiconv-1.16.tar.gz && cd libiconv-1.16
./configure --prefix=/usr/local/libiconv && make && make install

echo -e "\033[32m Install $Php_version : \033[0m"
sleep 3
cd /opt && wget $Url/php/linux/$Php_version.tar.gz && tar -zxvf $Php_version.tar.gz && cd $Php_version
./configure $Fileinfo --prefix=$Path --enable-fpm --with-fpm-user=www --with-fpm-group=www --with-zlib --with-zlib-dir --enable-mbstring  --enable-soap --enable-calendar --with-curl --disable-rpath --enable-inline-optimization  --enable-sockets --enable-mbregex --enable-bcmath --with-mhash --with-mysqli --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-pdo-mysql --with-openssl  --with-libdir=lib64 --enable-ftp --with-xmlrpc --enable-opcache --with-xpm-dir=/usr --with-pear --with-pdo-sqlite --enable-xml --with-xsl --enable-sysvsem --enable-shmop --with-gettext --enable-exif --enable-pcntl --enable-intl --with-kerberos --with-iconv=/usr/local/libiconv

echo ""
echo -e "\033[32m make php: \033[0m"
sleep 2
#make -j$Core ZEND_EXTRA_LIBS='-liconv'
make -j$Core
make -j$Core install
echo -e "\033[32m Make done: \033[0m"
sleep 2

echo '#!/bin/sh  
# chkconfig:   2345 15 95
# description:  PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation \

# with some additional features useful for sites of any size, especially busier sites.
# DateTime: 2016-09-20

# Source function library.  
. /etc/rc.d/init.d/functions  

# Source networking configuration.  
. /etc/sysconfig/network  

# Check that networking is up.  
[ "$NETWORKING" = "no" ] && exit 0  

phpfpm="/usr/local/php/sbin/php-fpm"  
prog=$(basename ${phpfpm})  

lockfile=/var/lock/subsys/phpfpm

start() {  
    [ -x ${phpfpm} ] || exit 5  
    echo -n $"Starting $prog: "  
    daemon ${phpfpm}
    retval=$?  
    echo  
    [ $retval -eq 0 ] && touch $lockfile  
    return $retval  
}  

stop() {  
    echo -n $"Stopping $prog: "  
    killproc $prog -QUIT  
    retval=$?  
    echo  
    [ $retval -eq 0 ] && rm -f $lockfile  
    return $retval  
}  

restart() {  
    configtest || return $?  
    stop  
    start  
}  

reload() {  
    configtest || return $?  
    echo -n $"Reloading $prog: "  
    killproc ${phpfpm} -HUP  
    RETVAL=$?  
    echo  
}  

force_reload() {  
    restart  
}  

configtest() {  
  ${phpfpm} -t
}  

rh_status() {  
    status $prog  
}  

rh_status_q() {  
    rh_status >/dev/null 2>&1  
}  

case "$1" in  
    start)  
        rh_status_q && exit 0  
        $1  
        ;;  
    stop)  
        rh_status_q || exit 0  
        $1  
        ;;  
    restart|configtest)  
        $1  
        ;;  
    reload)  
        rh_status_q || exit 7  
        $1  
        ;;  
    status)  
        rh_status  
        ;;  
    *)  
        echo $"Usage: $0 {start|stop|status|restart|reload|configtest}"
        exit 2
esac'>/etc/init.d/php-fpm

if [ "$Php_version" != "php-5.6.0" ]; then
  echo -e "\033[32m php-7+ \033[0m"
  cd $Path/etc/php-fpm.d/ && cp www.conf.default www.conf  
  sed -i 's/pm = dynamic/pm = static/' $Path/etc/php-fpm.d/www.conf
  sed -i 's/;listen.owner = www/listen.owner = www/' $Path/etc/php-fpm.d/www.conf
  sed -i 's/;listen.group = www/listen.group = www/' $Path/etc/php-fpm.d/www.conf
  sed -i 's/;listen.mode = 0660/listen.mode = 0660/' $Path/etc/php-fpm.d/www.conf  
 else
  echo -e "\033[32m php-5.6 \033[0m"
fi
cp $Path/etc/php-fpm.conf.default $Path/etc/php-fpm.conf
cp /opt/$Php_version/php.ini-development $Path/etc/php.ini
ln -s $Path/etc/php.ini $Path/lib/php.ini
ln -s $Path/bin/php    /usr/local/bin/php
sed -i 's/;opcache.enable=1/opcache.enable=1/' $Path/etc/php.ini
sed -i 's/;opcache.memory_consumption=128/opcache.memory_consumption=128/' $Path/etc/php.ini
sed -i 's/;opcache.validate_timestamps=60/opcache.validate_timestamps=60/' $Path/etc/php.ini
sed -i 's/short_open_tag = Off/short_open_tag = On/' $Path/etc/php.ini
sed -i 's/expose_php = On/expose_php = Off/' $Path/etc/php.ini
echo 'zend_extension="opcache.so"'>>$Path/etc/php.ini
sed -i "s:/usr/local/php:$Path:g" /etc/init.d/php-fpm

yum install -y libmcrypt-devel
cd /opt && wget $Url/php/module/zip-1.20.0.tgz && tar zxf zip-1.20.0.tgz && cd zip-1.20.0 && $Path/bin/phpize
./configure --with-php-config=$Path/bin/php-config
make && make install && echo 'extension="zip.so"'>>$Path/etc/php.ini

if [  $Php_num -ge 70 ]
then 
    #>=7.0 install redis module
	echo -e "\033[32m make php-redis: \033[0m"
    cd /opt && wget $Url/php/module/phpredis-develop.zip && unzip phpredis-develop.zip && cd phpredis-develop
    $Path/bin/phpize
    ./configure --with-php-config=$Path/bin/php-config
    make && make install && echo 'extension="redis.so"'>>$Path/etc/php.ini
	
    #>=7.0 install mcrypt module
	echo -e "\033[32m make mcrypt module: \033[0m"	
    cd /opt && wget $Url/php/module/mcrypt-1.0.4.tgz && tar zxvf mcrypt-1.0.4.tgz && cd mcrypt-1.0.4
    $Path/bin/phpize
    ./configure --with-php-config=$Path/bin/php-config
    make && make install && echo 'extension="mcrypt.so"'>>$Path/etc/php.ini	
	
    #>=7.0 install memcached and memcache module
	echo -e "\033[32m make memcached and memcache module: \033[0m"
	yum -y install libmemcached libmemcached-devel
    cd /opt && wget $Url/php/module/memcache-4.0.5.2.tgz && tar zxvf memcache-4.0.5.2.tgz && cd memcache-4.0.5.2
    $Path/bin/phpize
    ./configure --with-php-config=$Path/bin/php-config
    make && make install && echo 'extension="memcache.so"'>>$Path/etc/php.ini 
	
	cd /opt && wget $Url/php/module/memcached-3.1.5.tgz && tar zxvf memcached-3.1.5.tgz && cd memcached-3.1.5
    $Path/bin/phpize
    ./configure --with-php-config=$Path/bin/php-config
    make && make install && echo 'extension="memcached.so"'>>$Path/etc/php.ini 
	
	#>=7.0 install mongodb module
	echo -e "\033[32m make mongodb module: \033[0m"	
    cd /opt && wget $Url/php/module/mongodb-1.12.0.tgz && tar zxvf mongodb-1.12.0.tgz && cd mongodb-1.12.0
    $Path/bin/phpize
    ./configure --with-php-config=$Path/bin/php-config
    make && make install && echo 'extension="mongodb.so"'>>$Path/etc/php.ini
	
	#>=7.0 install gd module
	#cd /opt/$Php_version/ext/gd && $Path/bin/phpize && ./configure --with-php-config=$Path/bin/php-config && make && make install && echo 'extension="gd.so"'>>$Path/etc/php.ini
	
	#>=7.0 install  sodium
	cd /opt/$Php_version/ext/sodium && $Path/bin/phpize && ./configure --with-php-config=$Path/bin/php-config && make && make install && echo 'extension="sodium.so"'>>$Path/etc/php.ini
	
	#imagick
	
	cd /opt && wget $Url/php/module/imagick-3.7.0.tgz && tar zxvf imagick-3.7.0.tgz && cd imagick-3.7.0
	$Path/bin/phpize && ./configure --with-php-config=$Path/bin/php-config && make -j$Core && make -j$Core install && echo 'extension="imagick.so"'>>$Path/etc/php.ini
		
	
	else
	echo ""
fi


echo "net.ipv4.tcp_max_syn_backlog = 1048576 ">> /etc/sysctl.conf
echo "8192" > /proc/sys/net/core/somaxconn
echo "echo 8192 > /proc/sys/net/core/somaxconn">>/etc/rc.local
echo ""
echo -e "\033[32m Starting php-fpm... \033[0m"
sleep 2
chmod a+x /etc/init.d/php-fpm && systemctl daemon-reload
chkconfig --add /etc/init.d/php-fpm && chkconfig php-fpm on
service php-fpm start
echo -e "\033[32m Done. \033[0m"